
package com.william.killian.trivia;

import java.util.Collections;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;

import com.william.killian.trivia.model.Quiz;
import com.william.killian.trivia.util.Util;

public class ItemChooser extends ListActivity implements View.OnClickListener
{
    private String type;
    private ArrayAdapter<Quiz> mAdapter;
    
    public void onCreate (Bundle savedInstanceState)
    {
        super.onCreate (savedInstanceState);
        setTheme (android.R.style.Theme_Light);
        type = getIntent ().getStringExtra ("type");
        Collections.sort (Util.quizzes);
        mAdapter = new ArrayAdapter <Quiz> (this, android.R.layout.simple_list_item_1, Util.quizzes);
        if (type.equalsIgnoreCase ("edit"))
        {
            getListView ().addHeaderView (new Button (this) {{
                setText (R.string.app_quiz_creator);
                setOnClickListener (ItemChooser.this);
            }});
        }
        setListAdapter (mAdapter);
        Selecter<Quiz> selecter = new Selecter<Quiz> ();
        getListView ().setOnItemClickListener (selecter);
        if (type.equalsIgnoreCase ("edit"))
        {
            getListView ().setOnItemLongClickListener (selecter);
        }
    }
    
    public void onClick (View arg0)
    {
        startActivity (new Intent (this, QuizCreator.class));
        
    }

    @SuppressWarnings ("hiding")
    private class Selecter<Quiz> implements OnItemClickListener, OnItemLongClickListener
    {
        public void onItemClick (AdapterView<?> arg0, View v, int pos, long id)
        {
            Intent intent = new Intent ();
            if (type.equalsIgnoreCase ("quiz"))
            {
                intent = new Intent (ItemChooser.this, Trivia.class);
                intent.putExtra ("quiz", Util.quizzes.get (pos));
            }
            else if (type.equalsIgnoreCase ("hs"))
            {
                intent = new Intent (ItemChooser.this, HighScoreViewer.class);
                intent.putExtra ("highscore", Util.quizzes.get (pos).getHighScoreFile ());
            }
            else if (type.equalsIgnoreCase ("edit"))
            {
                intent = new Intent (ItemChooser.this, QuizEditor.class);
                intent.putExtra ("index", pos);
            }
            startActivity (intent);
            finish ();
        }

        public boolean onItemLongClick (final AdapterView<?> list, View v, final int pos, long id)
        {
            new AlertDialog.Builder (ItemChooser.this)
                .setTitle (R.string.title_quiz_delete)
                .setMessage (R.string.text_quiz_delete)
                .setPositiveButton (R.string.yes, new OnClickListener()
                {
                    public void onClick (DialogInterface dialog, int which)
                    {
                        Util.quizzes.get (pos - 1).getFile ().delete ();
                        Util.quizzes.remove (pos - 1);
                        mAdapter.notifyDataSetChanged ();
                    }
                })
                .setNegativeButton (R.string.no, null)
                .setCancelable (true)
                .show ();
            return true;
        }
    }
    
    public void onResume ()
    {
        super.onResume ();
        if (mAdapter != null)
        {
            mAdapter.notifyDataSetChanged ();
        }
    }
}
